package com.demarc.pesquisas.infraestrutura.hibernate.type;

import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.type.StringType;
import org.hibernate.usertype.UserType;

public abstract class AbstractBooleanCharType implements UserType {

	private static final StringType STRING = new StringType();

	public abstract String getSim();

	public abstract String getNao();

	/**
	 * {@inheritDoc}
	 */
	@Override
	public int[] sqlTypes() {
		return new int[] { Types.CHAR };
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	@SuppressWarnings("rawtypes")
	public Class returnedClass() {
		return Boolean.class;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean equals(Object object1, Object object2)
			throws HibernateException {
		return object1 == object2 // NOPMD Need to compare the references
				|| (object1 != null && object2 != null && object1
						.equals(object2));
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public int hashCode(Object object) throws HibernateException {
		return object.hashCode();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public Object nullSafeGet(ResultSet inResultSet, String[] names,
			SessionImplementor session, Object owner)
			throws HibernateException, SQLException {
		String value = STRING.nullSafeGet(inResultSet, names[0], session);
		return parse(value);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void nullSafeSet(PreparedStatement st, Object value, int index,
			SessionImplementor session) throws HibernateException, SQLException {
		String valor = toStringValue((Boolean) value);

		if (valor == null) {
			st.setNull(index, java.sql.Types.CHAR);
		} else {
			st.setString(index, valor);
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public Object deepCopy(Object object) throws HibernateException {
		return object;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean isMutable() {
		return false;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public Serializable disassemble(Object value) throws HibernateException {
		return (Boolean) value;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public Object assemble(Serializable cached, Object owner)
			throws HibernateException {
		return cached;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public Object replace(Object original, Object target, Object owner)
			throws HibernateException {
		return original;
	}

	private Boolean parse(String value) {
		Boolean result = null;

		if (value != null) {
			result = getNao().equals(value) ? false : true;
		}
		return result;
	}

	private String toStringValue(Boolean val) {
		String resulto = null;

		if (val != null) {
			resulto = val ? getSim() : getNao();
		}
		return resulto;
	}
}
